const KoaRouter = require("koa-router")

const { auth } = require('../middleware/auth.middleware')
const { validator,goodsIdIsNotNull,isHasStockNumber } = require("../middleware/cart.middleware")

const { add, findAll,update ,deleteCart,selectOrUnselect} = require("../controller/cart.controller")


const router = new KoaRouter({prefix:"/carts"})


// 登录  校验

/*

goods_id 是否存在 不存在 便不能添加
number数不能大于goods_num中的库存数量

*/

router.post('/',auth, validator({goods_id:'number'}),goodsIdIsNotNull,isHasStockNumber, add)


// 获取购物车列表

router.get('/',auth,findAll)

// 更新购物车

router.patch('/:id',auth,validator({
  number:{type:'number',required:false},
  selected:{type:'bool',required:false}
}),update)

// 删除购物车
router.delete('/',auth,deleteCart)

// 全选与全不选
router.post('/selectOrUnselect',auth,selectOrUnselect)

module.exports = router
